package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Override
    public void upload(List<OrderSetting> orderSettingList) {
        if(orderSettingList !=null && orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                long number=orderSettingMapper.findByDate(orderSetting.getOrderDate());
                if(number>0){
                    orderSettingMapper.updateByDate(orderSetting);
                }else {
                    orderSettingMapper.save(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByDate(String date) {
        String begin=date+"-1";
        String end=date+"-31";
        Map<String,String> map=new HashMap<String, String>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list=orderSettingMapper.getOrderSettingByDate(map);
        List<Map> mapList=new ArrayList<Map>();
        if(list != null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> map1=new HashMap<String,Object>();
                map1.put("date",orderSetting.getOrderDate().getDate());
                map1.put("number",orderSetting.getNumber());
                map1.put("reservations",orderSetting.getReservations());
                mapList.add(map1);
            }
        }
        return mapList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long number=orderSettingMapper.findByDate(orderSetting.getOrderDate());
        if(number>0){
            orderSettingMapper.updateByDate(orderSetting);
        }else {
            orderSettingMapper.save(orderSetting);
        }
    }
}
